import request from '@/utils/request'

// 系统监控数据接口
export interface SystemMonitorData {
  cpu: {
    usage: number
    cores: number
    temperature: number
    frequency: number
    name: string
    architecture: string
  }
  memory: {
    used: number
    total: number
    usage: number
    available: number
  }
  disk: {
    used: number
    total: number
    usage: number
    readSpeed: number
    writeSpeed: number
    drives: Array<{
      name: string
      fileSystem: string
      totalSize: number
      availableSpace: number
      usage: number
    }>
  }
  network: {
    uploadSpeed: number
    downloadSpeed: number
    latency: number
    packetsLost: number
    interfaces: Array<{
      name: string
      description: string
      status: string
      type: string
      bytesSent: number
      bytesReceived: number
    }>
  }
  system: {
    uptime: number
    processes: number
    threads: number
    loadAverage: number[]
    startTime: string
  }
  operatingSystem: {
    name: string
    version: string
    architecture: string
    platform: string
    machineName: string
    userName: string
    userDomainName: string
    dotNetVersion: string
  }
}

// 进程信息接口
export interface ProcessInfo {
  id: number
  name: string
  cpu: number
  memory: number
  status: string
  startTime: string
  threads: number
  handles: number
}

// 系统告警接口
export interface SystemAlert {
  id: string
  type: 'warning' | 'error' | 'info'
  level: string
  message: string
  source: string
  timestamp: string
  resolved: boolean
}

// 实时性能指标接口
export interface RealtimeMetrics {
  timestamp: string
  cpu: {
    usage: number
    temperature: number
    frequency: number
  }
  memory: {
    usage: number
    used: number
    available: number
  }
  disk: {
    usage: number
    readSpeed: number
    writeSpeed: number
  }
  network: {
    latency: number
    uploadSpeed: number
    downloadSpeed: number
    packetsLost: number
  }
}

// 系统健康状态接口
export interface SystemHealth {
  score: number
  status: 'excellent' | 'good' | 'warning' | 'critical'
  timestamp: string
  alerts: number
  uptime: number
  components: {
    cpu: 'healthy' | 'warning' | 'critical'
    memory: 'healthy' | 'warning' | 'critical'
    disk: 'healthy' | 'warning' | 'critical'
    network: 'healthy' | 'warning' | 'critical'
  }
}

const systemMonitorService = {
  /**
   * 获取系统监控概览数据
   */
  getSystemOverview: (): Promise<SystemMonitorData> => {
    return request.get('/system/systemmonitor/overview')
  },

  /**
   * 获取CPU信息
   */
  getCpuInfo: () => {
    return request.get('/system/systemmonitor/cpu')
  },

  /**
   * 获取内存信息
   */
  getMemoryInfo: () => {
    return request.get('/system/systemmonitor/memory')
  },

  /**
   * 获取磁盘信息
   */
  getDiskInfo: () => {
    return request.get('/system/systemmonitor/disk')
  },

  /**
   * 获取网络信息
   */
  getNetworkInfo: () => {
    return request.get('/system/systemmonitor/network')
  },

  /**
   * 获取操作系统信息
   */
  getOperatingSystemInfo: () => {
    return request.get('/system/systemmonitor/os')
  },

  /**
   * 获取进程列表
   */
  getProcessList: (top: number = 20): Promise<ProcessInfo[]> => {
    return request.get(`/system/systemmonitor/processes?top=${top}`)
  },

  /**
   * 获取系统告警列表
   */
  getSystemAlerts: (): Promise<SystemAlert[]> => {
    return request.get('/system/systemmonitor/alerts')
  },

  /**
   * 获取实时性能指标
   */
  getRealtimeMetrics: (): Promise<RealtimeMetrics> => {
    return request.get('/system/systemmonitor/metrics/realtime')
  },

  /**
   * 获取系统健康状态
   */
  getSystemHealth: (): Promise<SystemHealth> => {
    return request.get('/system/systemmonitor/health')
  }
}

export default systemMonitorService
